package tw.com.fetc.bms.core;

import com.caucho.hessian.client.HessianProxyFactory;
import java.net.MalformedURLException;
import java.util.HashMap;
import tw.com.fetc.bms.exception.BmsClientException;

// Referenced classes of package tw.com.fetc.bms.core:
//            VtpAcctManagerInterface

public class VtpAcctManagerFacade
{

    private String apiUrl;
    private HessianProxyFactory factory;
    private VtpAcctManagerInterface vtpAcctManager;
    
    public VtpAcctManagerFacade(String apiUrl) throws BmsClientException, MalformedURLException
    {
        if(apiUrl == null)
        {
            throw new BmsClientException("The BMS hessian api url string can not be null!");
        }
        else
        {
            this.apiUrl = apiUrl;
            factory = new HessianProxyFactory();
            vtpAcctManager = (VtpAcctManagerInterface)factory.create(tw.com.fetc.bms.core.VtpAcctManagerInterface.class, this.apiUrl);
            return;
        }
    }

    public HashMap queryAllVtpAcctList(HashMap params)
        throws BmsClientException
    {
        HashMap map = null;
        if(params.get("custId") == null)
            throw new BmsClientException("Input Parameter : VTP cust ID can not be null!");
        if(params.get("lprNbr") == null)
            throw new BmsClientException("Input Parameter : VTP lpr number can not be null!");
        try
        {
            map = vtpAcctManager.queryAllVtpAcctList(params);
        }
        catch(Exception ex)
        {
            throw new BmsClientException("Get the Connection with BMS hessian API to raise error : " + ex.getMessage(), ex);
        }
        return map;
    }

    public HashMap queryVtpTransLog(HashMap params)
        throws BmsClientException
    {
        HashMap map = null;
        if(params.get("acctId") == null)
            throw new BmsClientException("Input Parameter : VTP account ID can not be null!");
        if(params.get("transStartRowNo") == null || params.get("transEndRowNo") == null)
            throw new BmsClientException("Input Parameter : Start & End row number can not be null!");
        try
        {
            map = vtpAcctManager.queryVtpTransLog(params);
        }
        catch(Exception ex)
        {
            throw new BmsClientException("Get the Connection with BMS hessian API to raise error : " + ex.getMessage(), ex);
        }
        return map;
    }

    public HashMap getVtpAcctBalance(HashMap params)
        throws BmsClientException
    {
        HashMap map = null;
        if(params.get("acctId") == null)
            throw new BmsClientException("Input Parameter : VTP account ID can not be null!");
        try
        {
            map = vtpAcctManager.getVtpAcctBalance(params);
        }
        catch(Exception ex)
        {
            throw new BmsClientException("Get the Connection with BMS hessian API to raise error : " + ex.getMessage(), ex);
        }
        return map;
    }

    public HashMap addVtpAcctBalance(HashMap params)
        throws BmsClientException
    {
        HashMap map = null;
        if(params.get("acctId") == null)
            throw new BmsClientException("Input Parameter : VTP account ID can not be null!");
        if(params.get("txCategoryCode") == null)
            throw new BmsClientException("Input Parameter : Transation category code can not be null!");
        if(params.get("txCategoryCode").equals("Y") && params.get("txAmt") == null)
            throw new BmsClientException("Input Parameter : Transation amount can not be null!");
        if(params.get("txChannelId") == null)
            throw new BmsClientException("Input Parameter : Transation channel ID can not be null!");
        if(params.get("vaTxId") == null)
            throw new BmsClientException("Input Parameter : Transation ID can not be null!");
        if(params.get("barChkCode") == null)
            throw new BmsClientException("Input Parameter : Bar code can not be null!");
        if(params.get("txCategoryCode").equals("Z") && params.get("vaCancelTxId") == null)
            throw new BmsClientException("Input Parameter : Transation cancel ID can not be null!");
        try
        {
            map = vtpAcctManager.addVtpAcctBalance(params);
        }
        catch(Exception ex)
        {
            throw new BmsClientException("Get the Connection with BMS hessian API to raise error : " + ex.getMessage(), ex);
        }
        return map;
    }

    public HashMap genNewVtpAcct(HashMap params)
        throws BmsClientException
    {
        HashMap map = null;
        if(params.get("acctId") == null)
            throw new BmsClientException("Input Parameter : VTP account ID can not be null!");
        if(params.get("custId") == null)
            throw new BmsClientException("Input Parameter : VTP cust ID can not be null!");
        if(params.get("lprNbr") == null)
            throw new BmsClientException("Input Parameter : VTP lpr number can not be null!");
        if(params.get("applyDate") == null)
            throw new BmsClientException("Input Parameter : VTP acct apply date can not be null!");
        if(params.get("applyRealDate") == null)
            throw new BmsClientException("Input Parameter : VTP acct real apply date can not be null!");
        if(params.get("txChannelId") == null)
            throw new BmsClientException("Input Parameter : Transation channel ID Code can not be null!");
        if(params.get("txAmt") == null)
            throw new BmsClientException("Input Parameter : Transation amount can not be null!");
        if(params.get("vaTxId") == null)
            throw new BmsClientException("Input Parameter : ValueAdded transation ID can not be null!");
        try
        {
            map = vtpAcctManager.genNewVtpAcct(params);
        }
        catch(Exception ex)
        {
            throw new BmsClientException("Get the Connection with BMS hessian API to raise error : " + ex.getMessage(), ex);
        }
        return map;
    }

    public HashMap confirmVtpLprStatus(HashMap params)
        throws BmsClientException
    {
        HashMap map = null;
        if(params.get("acctId") == null)
            throw new BmsClientException("Input Parameter : VTP account ID can not be null!");
        if(params.get("lprStatus") == null)
            throw new BmsClientException("Input Parameter : VTP lpr status can not be null!");
        if(params.get("lprConfirmDate") == null)
            throw new BmsClientException("Input Parameter : VTP lpr confirm date can not be null!");
        try
        {
            map = vtpAcctManager.confirmVtpLprStatus(params);
        }
        catch(Exception ex)
        {
            throw new BmsClientException("Get the Connection with BMS hessian API to raise error : " + ex.getMessage(), ex);
        }
        return map;
    }

    public HashMap cancelVtpAcct(HashMap params)
        throws BmsClientException
    {
        HashMap map = null;
        if(params.get("acctId") == null)
            throw new BmsClientException("Input Parameter : VTP account ID can not be null!");
        if(params.get("txChannelId") == null)
            throw new BmsClientException("Input Parameter : Transation channel ID can not be null!");
        if(params.get("closeDate") == null)
            throw new BmsClientException("Input Parameter : VTP acct close date can not be null!");
        if(params.get("closeReason") == null)
            throw new BmsClientException("Input Parameter : VTP acct close reason can not be null!");
        if(!params.get("closeReason").equals("NORMAL") && !params.get("closeReason").equals("UNUSUAL"))
            throw new BmsClientException("Input Parameter : VTP acct close reason code must be 'NORMAL' or 'UNUSUAL' !");
        if(params.get("closeReason").equals("UNUSUAL") && params.get("lprStatus") == null)
            throw new BmsClientException("Input Parameter : VTP lpr status can not be null!");
        try
        {
            map = vtpAcctManager.cancelVtpAcct(params);
        }
        catch(Exception ex)
        {
            throw new BmsClientException("Get the Connection with BMS hessian API to raise error : " + ex.getMessage(), ex);
        }
        return map;
    }
}